/**
 * 2018年11月27日下午9:18:27
 * @author 谢进财
 */
package com.baihang.project.ds.controller;

import java.io.File;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baihang.framework.controller.BaseController;
import com.baihang.framework.kit.JsonKit;
import com.baihang.framework.kit.PathKit;
import com.baihang.project.ds.service.CustomerOneService;
import com.baihang.project.ds.service.LouDongService;

/**
 * 顾客 认筹1
 */
@Controller
@RequestMapping("/customerOne")
public class CustomerOneController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(CustomerOneController.class);
	@Autowired
	private CustomerOneService service;
	@Autowired
	private LouDongService service2;
	
	@RequestMapping
	public String index(Model model) {
		model.addAttribute("renchous", service2.getRenChou());	
		return "/project/ds/customerOne/index";
	}
	
	/**
	 * 管理员认筹
	 * @param page
	 * @param pagesize
	 * @param sortname
	 * @param sortorder
	 * @return
	 */
	@RequestMapping(value ="/getGrid1", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String getGrid1(int page,int pagesize,String sortname,String sortorder) {
		String grid = service.getGrid1(page,pagesize,sortname,sortorder);
		//System.out.println(grid);
		return grid;
	}
	/**
	 * 用户认筹
	 * @param page
	 * @param pagesize
	 * @param sortname
	 * @param sortorder
	 * @return
	 */
	@RequestMapping(value ="/getGrid2", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String getGrid2(int page,int pagesize,String sortname,String sortorder) {
		String grid = service.getGrid2(page,pagesize,sortname,sortorder);
		//System.out.println(grid);
		return grid;
	}
	
	@RequestMapping("/downloadCustomerOne")
	public ResponseEntity<byte[]> downloadCustomerOne(int type) throws Exception{	
		return service.downloadCustomerOne(type);
	}
	
	/**
	 * 上传认筹号
	 * @param roomexcel
	 * @return
	 */
	@RequestMapping(value ="/uploadCustomerOne", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String uploadCustomerOne(MultipartFile customerOne) {
		String filepath = PathKit.getWebRootPath()+"/upload/"+customerOne.getOriginalFilename();
		System.err.println(filepath);
		try {
			customerOne.transferTo(new File(filepath));
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return String.format("{\"result\":\"%s\",\"fileName\":\"%s\"}", "true",customerOne.getOriginalFilename());
	}
	
	/**
	 * 预览上传的excel
	 * @return
	 */
	@RequestMapping("/uploadcustomerOnePreview")
	public String uploadcustomerOnePreview(int type) {
		if(type==1) {
			return "/project/ds/customerOne/uploadClient1";			
		}else {
			return "/project/ds/customerOne/uploadClient2";	
		}
	}
	
	
	@RequestMapping(value ="/readCustomerOneByExcel", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String readCustomerOneByExcel(String fileName) throws Exception {
		System.err.println(fileName);
		String result = service.readCustomerOneByExcel(fileName);
		return result;
	}
	
	@RequestMapping(value ="/addGrid", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String addGrid(String json,int source) {
		String result ="true";
		String msg ="上传成功";
		if(!service.addGrid(json,source)) {
			result="false";
			msg="新增失败";
		}		
		return JsonKit.toDoubleJson(result, msg);
	}
	
	@RequestMapping(value ="/deleteCustomerOne", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String deleteCustomerOne(String tel) throws Exception {

		return service.deleteCustomerOne(tel);
	}
	@RequestMapping(value ="/deleteCustomerOneAll", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String deleteCustomerOneAll(String source) throws Exception {
		String result ="false" ;
		String msg ="删除失败";
		if(service.deleteCustomerOneAll(source)) {
			result ="true";
			msg="删除成功";
		}
		return JsonKit.toDoubleJson(result, msg);
	}
	
	@RequestMapping(value ="/saveCustomerOne", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String saveCustomerOne(String json) {
		String result ="true";
		String msg ="新增成功";
		if(!service.saveCustomerOne(json)) {
			result="false";
			msg="新增失败";
		}		
		return JsonKit.toDoubleJson(result, msg);
	}
	@RequestMapping(value ="/updateCustomerOne", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String updateCustomerOne(String json) {
		return service.updateCustomerOne(json);
	}
	@RequestMapping(value ="/updateCustomerOne2", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String updateCustomerOne2(String json) {
		
		return service.updateCustomerOne(json);
	}
	
	/**
	 * 修改管理用户 对价格显示的控件。state 1 显示价格  0隐藏价格
	 * @param state
	 * @param tel
	 * @return
	 */
	@RequestMapping(value ="/changeShowPrice", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String changeShowPrice(int state , String tel) {
		String result ="true";
		String msg ="操作成功";
		if(!service.changeShowPrice(state,tel)) {
			result="false";
			msg="操作失败";
		}		
		return JsonKit.toDoubleJson(result, msg);
	}
	/**
	 * 修改锁定用户  state 为 0 解锁(从redis中删除) 1 加锁(加入redis,相当于加入黑名单)
	 * @param state
	 * @param tel
	 * @return
	 */
	@RequestMapping(value ="/changeUserLock", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String changeUserLock(int state , String tel) {
		String result ="true";
		String msg ="操作成功";
		if(!service.changeUserLock(state,tel)) {
			result="false";
			msg="操作失败";
		}		
		return JsonKit.toDoubleJson(result, msg);
	}
	/**
	 * 根据手机号将用户踢到登录页并锁定
	 * @param tel
	 * @return
	 */
	@RequestMapping(value ="/userGetOut", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String userGetOut(String tel) {
		String result ="true";
		String msg =String.format("手机号%s用户被踢下线", tel);
		if(!service.userGetOut(tel)) {
			result="false";
			msg=String.format("手机号%s用户被踢下线失败", tel);
		}		
		return JsonKit.toDoubleJson(result, msg);
	}
}
